<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: qupen
  Date: 2016/11/9
  Time: 13:28
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:include page="../../include/head.jsp">
    <jsp:param name="title" value="产品录入信息"/>
</jsp:include>

<jsp:include page="../../include/tenant_header.jsp"/>

<jsp:include page="../../include/tenant_sidebar.jsp"/>
<div class="content-wrapper content-wrapper-bg">
    <section class="content-header">
        <h4 class="page-title" style="display: inline-block">产品录入列表</h4>
        <input id="time" hidden>

        <a id="previous" class="btn btn-add" style="margin-left: 80px">上周</a><a id="next" class="btn btn-add disabled"
                                                                                style="margin-left: 40px">下周</a>

        <div class="form-group has-success has-feedback" style="float: right;margin-right: 40px;">
            <input type="text" placeholder="搜索" class="form-control" id="inputSuccess2"
                   style="border-radius:5px;border-color:#aabfcb">
            <span class="glyphicon glyphicon-search form-control-feedback"></span>
        </div>
    </section>
    <section class="content">
        <div class="box box-primary table-header-line">
            <div class="box-body" style="padding: 0">
                <table class="table" id="device_list">
                    <thead>
                    <tr id="row">
                    </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
            </div>
        </div>
        <%--</div>--%>
    </section>
</div>
<jsp:include page="../../include/foot.jsp"/>

<script>
    $(document).ready(function () {
        var time = new Date();
        $("#time").val(time);
        getInfo(time);
    });

    $('#previous').click(function () {
        $('#next').removeClass("disabled");
        var time = dateAddSubtract(new Date($("#time").val()), -7);
        clearTable();
        $("#time").val(time);
        getInfo(time)
    });

    $('#next').click(function () {
        var time = dateAddSubtract(new Date($("#time").val()), 7);
        var timeNext = dateAddSubtract(new Date($("#time").val()), 8);
        if(timeNext >=  new Date()){
            $('#next').addClass("disabled");
        }
        clearTable();
        $("#time").val(time);
        getInfo(time)
    });

    function clearTable(){
        if ($('#device_list').hasClass('dataTable')) {
            var table = $('#device_list').dataTable();
            table.fnClearTable(); //清空一下table
            table.fnDestroy(); //还原初始化了的datatable
        }
        $("tbody").empty();
        $("#row").empty();
    }

    function getInfo(time) {
        $.ajax({
            url: "/partner/{tenant}/api/v1/product/info".replace("{tenant}", getKeyByUrl()),
            data: {
                time: time
            },
            success: function (data) {
                showTable(data);
            }
        });
    }

    function showTable(data) {
        var result = data.data;
        var times = data.time;
        if (times.length > 0) {
            $("#row").append($('<th id="device">机床id</th>'));
            $.each(times, function (index, time) {
                $('#row').append($('<th></th>').text(time))
            })
        }

        var t = $('#device_list').DataTable({
            "dom": '<"datatable-header"<"dataTables_filter">l><"datatable-scroll"t><"datatable-footer dataTable-footer-padding"<"pull-left"i><"pull-right"p>>',
            paging: true,
            lengthChange: false,
            ordering: true,
            autoWidth: false,
            info: true,
            serverSide: false,
            fixedHeader: true,
            searching: true,
            aLengthMenu: [15],
            "language": {url: '/lang/datatable.chs.json'}
        });

        $('#inputSuccess2').bind('keyup', function (e) {
            t.search(this.value).draw();
        });

        for (var i = 0; i < result.length; i++) {

            var oneDay = showInfoState(result[i][times[0]], result[i].deviceId, result[i].uniqueId, times[0]);
            var twoDay = showInfoState(result[i][times[1]], result[i].deviceId, result[i].uniqueId, times[1]);
            var threeDay = showInfoState(result[i][times[2]], result[i].deviceId, result[i].uniqueId, times[2]);
            var fourDay = showInfoState(result[i][times[3]], result[i].deviceId, result[i].uniqueId, times[3]);
            var fiveDay = showInfoState(result[i][times[4]], result[i].deviceId, result[i].uniqueId, times[4]);
            var sixDay = showInfoState(result[i][times[5]], result[i].deviceId, result[i].uniqueId, times[5]);
            var sevenDay = showInfoState(result[i][times[6]], result[i].deviceId, result[i].uniqueId, times[6]);

            t.row.add([
                result[i].uniqueId,
                oneDay,
                twoDay,
                threeDay,
                fourDay,
                fiveDay,
                sixDay,
                sevenDay
            ]).draw();
        }
    }

    function showInfoState(day, deviceId, uniqueId, time) {
        var url = "/partner/{tenant}/device/addDate?deviceId=" + deviceId + "&uniqueId=" + uniqueId + "&time=" + time;
        url = url.replace("{tenant}", getKeyByUrl());

        if (day == undefined) {
            day = '<a href="' + url + '" class="label label-default">未录入</a>';
        } else if (day == 'part') {
            day = '<a href="' + url + '" class="label label-warning">部分录入</a>';
        } else if (day == 'all') {
            day = '<a href="' + url + '" class="label label-success">全部录入</a>';
        }

        return day;
    }

</script>